import is from './is'

var defaultOptions = {
  // behaviors
  mute: false,
  autoplay: false,
  repeat: false,
  playbackRateControls: false,
  playbackRates: [0.25, 0.75, 1, 1.25],
  contextMenu: [], // todo format it

  // apperance
  controls: true,
  aspectratio: '',
  height: 360,
  width: 480,
  displaytitle: true,
  displaydescription: true,
  stretching: 'uniform',
  qualityLabels: '', // dash中不同带宽显示的文字: 如2500-HD, 1000-SD

  // preload
  preload: 'none',

  // playlist
  playlist: [],

  // skin
  skin: {
    name: 'default',
    url: ''
  },

  // logo
  logo: {
    file: '',
    hide: false,
    link: '',
    marginw: '3%',
    marginh: '6%',
    width: '15%',
    height: '10%',
    position: 'top-right'
  },

  // 进度条中高亮的点
  progressHighlights: [],

  // localization
  localization: {},

  // 需要加载video标签上的attributes或者properties
  videoProps: {}
}

function mergeOptions (options1, options2) {
  const options = {...options1, ...options2}

  // playbackRates
  if (is('array', options2.playbackRates)) {
    options.playbackRates = options2.playbackRates
      .filter(v => v > 0)
      .map(v => +v)
  }

  // playlist
  options.playlist = options2.playlist

  // skin
  options.skin = {...options1.skin, ...options2.skin}

  // logo
  options.logo = {...options1.logo, ...options2.logo}

  // localization
  options.localization = {...options1.localization, ...options2.localization}

  return options
}

function formatPlaylist (playlist) {
  // format playlist options
  if (is('array', playlist)) {
    const ret = []
    playlist.forEach(playitem => {
      const item = {}
      copyDefinedAttr(item, playitem, [
        'isLive',
        'image',
        'description',
        'title'
      ])

      // [{file: ''}]
      if (is('string', playitem.file) && playitem.file !== '') {
        item.sources = [
          {
            file: playitem.file
          }
        ]
        copyDefinedAttr(item.sources[0], playitem, ['type'])
        ret.push(item)

        // [{sources: [{file: ''}]}]
      } else if (
        is('array', playitem.sources) &&
        is('object', playitem.sources[0]) &&
        is('string', playitem.sources[0].file) &&
        playitem.sources[0].file !== ''
      ) {
        item.sources = []
        playitem.sources.forEach(sour => {
          if (is('string', sour.file) && sour.file !== '') {
            const source = {file: ''}
            copyDefinedAttr(source, sour, ['type'])
            item.sources.push(source)
          }
        })
        ret.push(item)
      }
    })

    if (
      is('array', ret) &&
      is('object', ret[0]) &&
      is('array', ret[0].sources) &&
      is('object', ret[0].sources[0]) &&
      is('string', ret[0].sources[0].file) &&
      ret[0].sources[0].file !== ''
    ) {
      return ret
    }
  }
}

function formatOptions (options) {
  const removeKeys = [
    'type',
    'isLive',
    'file',
    'image',
    'description',
    'title',
    'playlist'
  ]
  const ret = {}

  // copy some options
  for (let i in options) {
    if (removeKeys.indexOf(i) < 0 && options !== undefined) {
      ret[i] = options[i]
    }
  }

  // format playlist
  if (is('array', options.playlist)) {
    ret.playlist = formatPlaylist(options.playlist)
  } else if (is('string', options.file) && options.file) {
    const listItem = {
      file: options.file
    }
    copyDefinedAttr(listItem, options, [
      'type',
      'isLive',
      'image',
      'description',
      'type'
    ])

    const sourceItem = {
      file: options.file,
      label: 'default'
    }

    listItem.sources = [sourceItem]

    ret.playlist = [listItem]
  }

  return ret
}

function copyDefinedAttr (target, source, attributes) {
  attributes = attributes || Object.keys(target)
  attributes.forEach(attr => {
    if (!is('undefined', source[attr])) {
      target[attr] = source[attr]
    }
  })
}

export {defaultOptions, mergeOptions, formatOptions, formatPlaylist}
